define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
			//修改顶部按钮弹窗大小
			$(".btn-add").data("area",["70%","90%"])
			// $(".btn-edit").data("area",["70%","90%"])
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'integral_goods/index' + location.search,
                    add_url: 'integral_goods/add',
                    edit_url: 'integral_goods/edit',
                    del_url: 'integral_goods/delete',
                    multi_url: 'integral_goods/multi',
                    table: 'integral_goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('Id')},

                        {field: 'goods_name', title: __('Name'), operate: 'LIKE'},
                        {field: 'price', title: __('积分'),operate:false},
                        {field: 'is_enable', title: __('状态'),searchList:{"1":__('开启'),"0":__('关闭')}, formatter: Table.api.formatter.toggle},
                        {field: 'add_time', title: __('添加时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
				//禁用默认搜索
				search: false,
				//启用普通表单搜索
				commonSearch: true,
				//可以控制是否默认显示搜索单表,false则隐藏,默认为false
				searchFormVisible: true,
				showSearch: false,
            });
			table.on('post-body.bs.table',function () {
			    $('.btn-editone').data("area",["70%","90%"]);
			}),
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
			var i = 1;
			//之前的添加规格值
			$('#space').on('click', '.btn-primary', function (e) {
				var value = $(this).parent().prev().children()[0].value;
				// var value = $(this).parent().prev().prev().prev().children()[0].value;
				// console.log($(this).parent().prev().prev().prev());
				if (value == '') {
					layer.msg('规格值不能为做空');
					return false;
				}
				var space_list = `<div class="spaceItem">
									<div class="form-group skuMain">
											<label class="control-label col-xs-12 col-sm-2">规格:</label>
											<div class="col-xs-6 col-sm-6"><input type="text" data-rule="required" name="attr[]" maxlength="22" placeholder="请输入规格名称"  class="form-control"></div>
										<div class="col-xs-4 col-sm-2" style="text-align: right;"><span class="btn btn-danger move" >移除</span> </div>
										<div class="col-xs-8 col-sm-8 col-xs-offset-2 col-sm-offset-2 skuChildrenBox"></div>
									</div>
									<div class="form-group skuMain">
										<label class="control-label col-xs-12 col-sm-2">积分:</label>
										<div class="col-xs-6 col-sm-6"><input type="number" data-rule="required" name="price[]" maxlength="22" placeholder="请输入积分"  class="form-control"></div>
									</div>
									<div class="form-group skuMain">
											<label for="c-attr${i}" class="control-label col-xs-12 col-sm-2">规格图片:</label>
											<div class="col-xs-12 col-sm-8">
												<div class="input-group">
													<input id="c-attr${i}" data-rule="required" class="form-control" size="50" name="images[]" type="text">
													<div class="input-group-addon no-border no-padding">
														<span><button type="button" id="plupload-attr${i}" class="btn btn-danger plupload" data-input-id="c-attr${i}" data-mimetype="image/gif,image/jpeg,image/png,image/jpg,image/bmp" data-multiple="false" data-preview-id="p-attr${i}"><i class="fa fa-upload"></i> 上传</button></span>
														<span><button type="button" id="fachoose-attr${i}" class="btn btn-primary fachoose" data-input-id="c-attr${i}" data-mimetype="image/*" data-multiple="false"><i class="fa fa-list"></i> 选择</button></span>
													</div>
													<span class="msg-box n-right" for="c-attr${i}"></span>
												</div>
												<ul class="row list-inline plupload-preview" id="p-attr${i}"></ul>
											</div>
									</div>
								</div>`;
				$('#space').append(space_list);
				i++;
				require(['upload'], function (Upload) {
					Upload.api.plupload($(".plupload"), function () {
						$(".btn-refresh").trigger("click");
					});
				});
			});
			//移除规格值
			//移除规格
			$('#space').on('click', '.btn-danger', function (e) {
				//判断是否是最后一个规格了
				// if($('.skuMain').length == 1){
				// 	layer.msg('规格值不能为做空');
				// 	return false;
				// }else{
				$(this).parent().parent().remove();
				// }
				
			});
            
            Controller.api.bindevent();
        },
        edit: function () {
            //添加规格值
        	var i = 0;
            $('#space').on('click', '.btn-primary', function (e) {
        		if(i>0){
        			i++
        		}else{
        			i = $(this).attr('data');
        			i++
        		}
        		
        		
            	var value = $(this).parent().prev().children()[0].value;
            	if (value == '') {
            		layer.msg('规格值不能为做空');
            		return false;
            	}
            	var space_list = `<div class="spaceItem">
            						<div class="form-group skuMain">
            								<label class="control-label col-xs-12 col-sm-2">规格:</label>
            								<div class="col-xs-6 col-sm-6"><input type="text" data-rule="required" name="attr[]" maxlength="22" placeholder="请输入规格名称"  class="form-control"></div>
            							<div class="col-xs-4 col-sm-2" style="text-align: right;"><span class="btn btn-danger move" >移除</span> </div>
            							<div class="col-xs-8 col-sm-8 col-xs-offset-2 col-sm-offset-2 skuChildrenBox"></div>
            						</div>
            						<div class="form-group skuMain">
            							<label class="control-label col-xs-12 col-sm-2">积分:</label>
            							<div class="col-xs-6 col-sm-6"><input type="number" data-rule="required" name="price[]" maxlength="22" placeholder="请输入积分"  class="form-control"></div>
            						</div>
            						<div class="form-group skuMain">
            								<label for="c-attr${i}" class="control-label col-xs-12 col-sm-2">规格图片:</label>
            								<div class="col-xs-12 col-sm-8">
            									<div class="input-group">
            										<input id="c-attr${i}" data-rule="required" class="form-control" size="50" name="images[]" type="text">
            										<div class="input-group-addon no-border no-padding">
            											<span><button type="button" id="plupload-attr${i}" class="btn btn-danger plupload" data-input-id="c-attr${i}" data-mimetype="image/gif,image/jpeg,image/png,image/jpg,image/bmp" data-multiple="false" data-preview-id="p-attr${i}"><i class="fa fa-upload"></i> 上传</button></span>
            											<span><button type="button" id="fachoose-attr${i}" class="btn btn-primary fachoose" data-input-id="c-attr${i}" data-mimetype="image/*" data-multiple="false"><i class="fa fa-list"></i> 选择</button></span>
            										</div>
            										<span class="msg-box n-right" for="c-attr${i}"></span>
            									</div>
            									<ul class="row list-inline plupload-preview" id="p-attr${i}"></ul>
            								</div>
            						</div>
            					</div>`;
            	$('#space').append(space_list);
        		require(['upload'], function (Upload) {
        			Upload.api.plupload($(".plupload"), function () {
        				$(".btn-refresh").trigger("click");
        			});
        		});
            });
            //移除规格值
            //移除规格
            $('#space').on('click', '.move', function (e) {
            	//判断是否是最后一个规格了
            	// if($('.skuMain').length == 1){
            	// 	layer.msg('规格值不能为做空');
            	// 	return false;
            	// }else{
            	$(this).parent().parent().parent().remove();
            	// }
            	
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});